package cn.lingyangwl.agile.auth.model.entity;

import cn.lingyangwl.agile.model.constants.ValidationCons;
import cn.lingyangwl.agile.model.BasePO;
import cn.lingyangwl.agile.model.constants.UserCons;
import cn.lingyangwl.framework.data.mate.annotations.FieldSensitive;
import cn.lingyangwl.framework.data.mate.fieldsensitive.enums.StrategyType;
import cn.lingyangwl.framework.mybatis.mate.annotations.FieldUnique;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 本地登录-自己用户名、密码登录，手机号码登录信息记录(目前没有使用, 可能后期会使用)
 *
 * @author shenguangyang
 */
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("auth_local_user")
public class LocalUser extends BasePO {
    @TableId
	private Long authId;
	
	/**
	 * 用户名
	 */
	@FieldUnique(message = "用户名已存在")
	private String userName;
	
	/**
	 * 用户id
	 */
	@NotNull(message = ValidationCons.NOT_EMPTY)
	private Long userId;
	
	/**
	 * 密码
	 */
	@FieldSensitive(strategy = StrategyType.EMPTY)
	private String password;
	
	/**
	 * 手机号
	 */
	private String phone;
	
	/**
	 * 邮箱
	 */
	@FieldSensitive(strategy = StrategyType.EMAIL)
	private String email;

	/**
	 * @see UserCons.Status
	 */
	private String status;

	/**
	 * 应用类型
	 * @see cn.lingyangwl.agile.model.constants.AppType
	 */
	@NotEmpty(message = ValidationCons.NOT_EMPTY)
	private String appType;

	/**
	 * 创建时间
	 */
	@TableField(fill = FieldFill.INSERT)
	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date createdAt;

	/**
	 * 更新时间
	 */
	@TableField(fill = FieldFill.UPDATE)
	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date updatedAt;


	public static LambdaQueryWrapper<LocalUser> lqw() {
		return Wrappers.lambdaQuery();
	}

	public boolean isAdmin() {
		return isAdmin(this.userId);
	}

	public static boolean isAdmin(Long userId) {
		return UserCons.ADMIN_ID.equals(userId);
	}
}
